// +build !appengine
/*
 * Copyright (C) 2013 rdrrlabs gmail com,
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package datastore

import (
    "appengine"
    "errors"
    "fmt"
    "strings"
)

var ErrNoSuchEntity = errors.New("datastore.ErrNoSuchEntity")

//----

type datastoreMockResulter interface {
    appengine.MockResulter
    AppendProperty(p Property)
}

type DatastoreMockResult struct {
    PutProperties PropertyList
}

func (d *DatastoreMockResult) AsString() string {
    results := make([]string, 0)
    for _, p := range d.PutProperties {
        results = append(results, p.AsString())
    }
    return strings.Join(results, "\n")
}

func (d *DatastoreMockResult) AppendProperty(p Property) {
    d.PutProperties = append(d.PutProperties, p)
}

//----

type Key struct {
}

func NewKey(c appengine.Context, kind, stringID string, intID int64, parent *Key) *Key {
    return nil
}

//-----
func Get(c appengine.Context, key *Key, dst interface{}) error {
    return nil
}

func Put(c appengine.Context, key *Key, src interface{}) (*Key, error) {

    var d datastoreMockResulter
    d = c.MockResult.(datastoreMockResulter)

    if saver, ok := src.(PropertyLoadSaver); ok {
        ch := make(chan Property)
        go saver.Save(ch)
        for p := range ch {
            d.AppendProperty(p)
        }
    }

    return nil, nil
}

func Delete(c appengine.Context, key *Key) error {
    return nil
}

//----
type Query struct {
}

func NewQuery(kind string) *Query {
    return nil
}

func (q *Query) Count(c appengine.Context) (int, error) {
    return 0, nil
}

func (q *Query) GetAll(c appengine.Context, dst interface{}) ([]*Key, error) {
    return nil, nil
}

func (q *Query) Limit(limit int) *Query {
    return nil
}

func (q *Query) Offset(offset int) *Query {
    return nil
}

func (q *Query) Order(fieldName string) *Query {
    return nil
}

func (q *Query) Run(c appengine.Context) *Iterator {
    return nil
}

//------

type Iterator struct {
}

type Cursor struct {
}

func (t *Iterator) Cursor() (Cursor, error) {
    return Cursor{}, nil
}

// Done is returned when a query iteration has completed.
var Done = errors.New("datastore: query has no more results")

func (t *Iterator) Next(dst interface{}) (*Key, error) {
    return nil, nil
}

//------

type Property struct {
    Name string
    Value interface{}
    NoIndex bool
    Multiple bool
}

func (p *Property) AsString() string {
    return fmt.Sprintf("[%v] index:%v, multi:%v -- %v", p.Name, !p.NoIndex, p.Multiple, p.Value)
}

type PropertyLoadSaver interface {
    Load(<-chan Property) error
    Save(chan<- Property) error
}

type PropertyList []Property

//------

type TransactionOptions struct {
    XG bool
}

func RunInTransaction(c appengine.Context, f func(tc appengine.Context) error, opts *TransactionOptions) error {
    return nil
}
